const util = require('../../utils/util.js');
const app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    region: ['广东省', '广州市', '海珠区'],
    isflag:0,
    address: { name: '', phone: '', province: '', city: '', district:'',address:''}
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    
  },
  conatact:function(e){
    var value = e.detail.value;
    var address = this.data.address;
    address.name = value;
    var tempaddress = address;
    this.setData({
      address:tempaddress
    })
  },
  settel:function(e){
    var value = e.detail.value;
    var address = this.data.address;
    address.phone = value;
    var tempaddress = address;
    this.setData({
      address: tempaddress
    })

  },
  setaddress:function(e){
    var value = e.detail.value;
    var address = this.data.address;
    address.address = value;
    var tempaddress = address;
    this.setData({
      address: tempaddress
    })
    // console.log(address)
  },
  bindRegionChange: function (e) {
    var value = e.detail.value;
    var address = this.data.address;
    address.province = value[0];
    address.city = value[1];
    address.district = value[2];
    var tempaddress = address;
    this.setData({
      region: e.detail.value,
      isflag:1,
      address: tempaddress
    })
   
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
    
  },
  save:function(){
    var user = app.globalData.userInfo;
    this.data.address.user_id = user.id;
    var data = this.data.address;
    var flag = this.validation(data);
    if(flag == 1){
      util.commonRequest("index/Address/addAddress",'post', data, function (res) {
        if(res.data.code==200){
          wx.showToast({
            title: '添加成功',
          })
          wx.navigateBack({
            delta: 1
          })
        }else{
          wx.showToast({
            icon:"none",
            title: res.data.msg,
          })
        }
      });
    }
  },
  validation:function(data){
    if (!data.name) {
      wx.showToast({
        icon: "none",
        title: '收货人不能为空',
      })
      return 0;
    }
    if (!data.phone) {
      wx.showToast({
        icon: "none",
        title: '联系电话不能为空',
      })
      return 0;
    }
    if (!data.province) {
      wx.showToast({
        icon: "none",
        title: '请选择地区',
      })
      return 0;
    }
    if (!data.address) {
      wx.showToast({
        icon: "none",
        title: '详细地址不能为空',
      })
      return 0;
    }
    return 1;
  },
  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
    
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
    
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
    
  }
})